package com.lzh.queue;

import java.util.Scanner;

/**
 * @author liuzhihao
 * @create 2021-10-08 21:25:23
 */
public class QueueExer {
    public static void main(String[] args) {
        Queue queue = new Queue(5);
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("1.入队(add)");
            System.out.println("2.出队(out)");
            System.out.println("3.显示所有队列(list)");
            System.out.println("4.显示队头数据(showHead)");
            System.out.println("5.退出");
            System.out.println("请输入你的操作：");
            int key = sc.nextInt();
            switch (key) {
                case 1:
                    System.out.print("请输入你要添加的数：");
                    int num = sc.nextInt();
                    queue.addQueue(num);
                    break;
                case 2:
                    queue.outQueue();
                    break;
                case 3:
                    queue.listQueue();
                    break;
                case 4:
                    int head = queue.showHead();
                    System.out.println(head);
                    break;
                case 5:
                    loop = false;
                    sc.close();
                    System.out.println("退出了");
                    break;
                default:
                    break;
            }
        }
    }
}

class Queue {
    int maxSize;
    int font = -1;
    int rear = -1;
    int[] arrQueue;

    public Queue(int maxSize) {
        this.maxSize = maxSize;
        arrQueue = new int[maxSize];
    }

    /**
     * 判断是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return font == rear;
    }

    /**
     * 判断是否为满
     *
     * @return
     */
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    /**
     * 添加
     */
    public void addQueue(int num) {
        if (isFull()) {
            throw new RuntimeException("队列已满");
        }
        rear++;
        arrQueue[rear] = num;
    }

    /**
     * 出队列
     *
     * @return
     */
    public int outQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        font++;
        return arrQueue[font];
    }

    /**
     * 显示
     */
    public void listQueue() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        int temp = font;
        while (true) {
            temp++;
            System.out.printf("arrQueue[%d] = %d\n", temp, arrQueue[temp]);
            if (temp >= rear) {
                break;
            }
        }
    }

    /**
     * 显示队列头的数据
     */
    public int showHead() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arrQueue[font + 1];
    }

}


